import 'package:demo/pages/markdown_editor/markdown_editor_cubit.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_htmlful/Div.dart';
import 'package:flutter_htmlful/Style.dart';
import 'package:markdown_editor_plus/markdown_editor_plus.dart';

class MarkdownEditorPage extends StatefulWidget {
  const MarkdownEditorPage({super.key});

  @override
  State<MarkdownEditorPage> createState() => _MarkdownEditorPageState();
}

class _MarkdownEditorPageState extends State<MarkdownEditorPage> with WidgetsBindingObserver {
  final TextEditingController _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => MarkdownEditorCubit(),
      child: BlocBuilder<MarkdownEditorCubit, MarkdownEditorState>(
        builder: (BuildContext context, MarkdownEditorState state) {
          return SafeArea(
            child: Scaffold(
              // resizeToAvoidBottomInset: false,
              body: Div(
                style: Style(
                  backgroundColor: Colors.white,
                  height: double.infinity,
                  scrollable: true,
                ),
                body: [
                  // editable text without toolbar
                  // editable text with toolbar by default
                  MarkdownAutoPreview(
                    controller: _controller,
                    emojiConvert: true,
                    enableToolBar: true,
                  ),

                  ElevatedButton(
                    onPressed: () {},
                    child: Text('发送'),
                  ),
                  Padding(padding: MediaQuery.of(context).viewInsets),
                ],
              ),
            ),
          );
        },
      ),
    );
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    WidgetsBinding.instance.removeObserver(this);
  }

  @override
  void didChangeMetrics() {
    super.didChangeMetrics();
    var size = MediaQuery.of(context).size.height;
    debugPrint("didChangeMetrics $size");
  }
}
